package com.travelDevelop.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.travelDevelop.commons.bean.SysFile;
import com.travelDevelop.commons.util.StringUtil;
import com.travelDevelop.commons.util.WebConst;
import com.travelDevelop.dao.FileMapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.*;

/**
 * 首页
 * 
 * @author shen.lang
 *
 */
@Controller
@RequestMapping("")
public class IndexController {
	

	@Autowired
	FileMapper fileMapper;
	
	
	
	@RequestMapping("")
	public String index(HttpServletRequest request, ModelMap map) {

		
		
		return "index";
	}

	@RequestMapping("/index")
	public String homePage(HttpServletRequest request, ModelMap map) {

		 String page = request.getParameter("page");
		 
		 if(page!=null&&page.equals("company")) {
			 return "index2";
		 }else if(page!=null&&page.equals("contact")) {
			 return "index3";
		 }

		return "index";
	}
	
	
	/**
	* 功能:为历史订单查询功能提供数据和条件查询
	* @param :
	* @reuslt: 
	* @throws:  
	* @author:redfly 
	*/
	@RequestMapping("/getAjaxData")
	@ResponseBody
	public Map<String, Object> getAjaxData(HttpServletRequest request) {

		Map<String, Object> responseMap = new HashMap<>();
		//获取参数
		String a = request.getParameter("theme");
		String b = request.getParameter("start_time");
		String c = request.getParameter("end_time");
		String page = request.getParameter("cPage");
		String size = request.getParameter("pSize");
		String imgFile = request.getParameter("imgFile");

		SysFile sysFile = new SysFile();
		
		//注入实体类，将实体对象作为参数传入mybatis的mapper
		sysFile.setStartTime(b);
		sysFile.setEndTime(c);
		sysFile.setFileName(a);
		
		//根据当前页数设置分页偏移量，还有根据管理员自定义的每页条数设置limit
		sysFile.setLimit(size);
		if(!StringUtil.isNull(page)&&!StringUtil.isNull(size)){
		sysFile.setOffset((Integer.valueOf(page)-1)*Integer.valueOf(size));
		}else {
			sysFile.setOffset(0);
			//sysFile.setLimit("10");
		}
		HttpSession session = request.getSession();
		String roleStr = (String) session.getAttribute(WebConst.SESSION_LOGIN_USER_ROLE);
		if (roleStr==null||Integer.parseInt(roleStr)>0) {
			sysFile.setUploadUser((String) session.getAttribute(WebConst.SESSION_LOGIN_USER_ID));
		}
		List<SysFile> sfList;
		if(imgFile==null||imgFile =="") {
		sfList = fileMapper.selectOrderFile(sysFile);
		responseMap.put("totals",fileMapper.countByOrder(sysFile));
		}else {
		sfList = fileMapper.selectImgFile(sysFile);
		responseMap.put("totals",fileMapper.countByImg(sysFile));
		}
		
		//把数据库时间转格式，方便前端展示
		for(SysFile file:sfList) {
		String show =file.getUploadTime().toString().replace("T", "  "); 
		file.setShowTime(show);
		}
        // 使用 replace 方法将 'T' 替换为两个空格
		
		//responseMap.put("totals",fileMapper.countByOrder(sysFile));
		responseMap.put("data",sfList);
		responseMap.put("role",session.getAttribute(WebConst.SESSION_LOGIN_USER_ROLE));

		return responseMap;
	}
	
	

}